/**
  ******************************************************************************
  * @file    _tc32l010_adc.h
  * @author  CHIPAT Application Team
  * @brief   This file contains all the functions prototypes for the ADC firmware
  *          library
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __XS32L010_ADC_H
#define __XS32L010_ADC_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "tc32l010.h"
#include "tc32l010_rcc.h"

/** @addtogroup XS32L010_HAL_Driver
  * @{
  */

/* Exported types ------------------------------------------------------------*/
/** @defgroup ADC_Exported_Types ADC Exported Types
  * @{
  */

/** 
  * @brief  ADC group regular oversampling structure definition
  */
typedef struct
{

  FunctionalState ADC_ContinuousConvMode;   /*!< Specifies whether the conversion is performed in
                                                 Continuous or Single mode.
                                                 This parameter can be set to ENABLE or DISABLE. */

  uint8_t ADC_TrigConv;                     /*!< Defines the trigger used to start the analog
                                                 to digital conversion of regular channels. This parameter
                                                 can be a value of @ref ADC_trigger_sources_for_channels_conversion */

  uint8_t ADC_CMPGain;                      /*!< comp gain value */

  uint8_t ADC_AccEn;                        /*!< Acc the signale channel value */

  uint8_t ADC_Ratio;                        /*!< Config the ADC ratio */

  uint8_t ADC_Cnt;                          /*!< Config the Continuous conversion numbers */

  uint8_t ADC_FirstSampleTime;              /*!< Config the First sample time */ 

  uint8_t ADC_Offset;                       /*!< Config the offset */

}ADC_InitTypeDef;


/** @defgroup ADC_Exported_Constants
  * @{
  */ 
#define IS_ADC_ALL_PERIPH(PERIPH)            ((PERIPH) == ADC1)


#define ADC_CMP_LOWGAIN                      ((uint8_t)0x00)
#define ADC_CMP_HIGHGIAN                     ((uint8_t)0x01)
#define IS_ADC_CMPGAIN(GAIN)                 (((GAIN) == ADC_CMP_LOWGAIN) || ((GAIN) == ADC_CMP_HIGHGIAN))

#define IS_ADC_OFFSET(OFFSET)                (((OFFSET) >= 0x00) && ((OFFSET) <= 0x3F))

#define ADC_FIRST_SAMPLE_MIN                 ((uint8_t)0x03)
#define ADC_FIRST_SAMPLE_MAX                 ((uint8_t)0x0F)
#define IS_ADC_FIRST_SAMPLE(TIME)            (((TIME) >= ADC_FIRST_SAMPLE_MIN) && ((TIME) <= ADC_FIRST_SAMPLE_MAX))

#define ADC_RATIO_12BIT                      ((uint8_t)0x00)
#define ADC_RATIO_10BIT                      ((uint8_t)0x01)
#define IS_ADC_RATIO(RATIO)                  (((RATIO) == ADC_CMP_LOWGAIN) || ((RATIO) == ADC_CMP_HIGHGIAN))

#define IS_ADC_CONTINUOUS_CONV_CNT(CNT)      ((CNT) <= 0xFF)


/** @defgroup ADC_trigger_sources_for_channels_conversion
  * @{
  */ 
#define ADC_TRIGS_SEL_NONE                   ((uint8_t)0x00)
#define ADC_TRIGS_SEL_TIM4                   ((uint8_t)0x01)
#define ADC_TRIGS_SEL_TIM5                   ((uint8_t)0x02)
#define ADC_TRIGS_SEL_TIM1_CC                ((uint8_t)0x03)
#define ADC_TRIGS_SEL_LPTIM                  ((uint8_t)0x04)
#define ADC_TRIGS_SEL_TIM1_TRGO              ((uint8_t)0x05)
#define ADC_TRIGS_SEL_TIM2_TRGO              ((uint8_t)0x06)
#define ADC_TRIGS_SEL_TIM2                   ((uint8_t)0x07)
#define ADC_TRIGS_SEL_USART0                 ((uint8_t)0x08)
#define ADC_TRIGS_SEL_USART1                 ((uint8_t)0x09)
#define ADC_TRIGS_SEL_LPUART                 ((uint8_t)0x0A)
#define ADC_TRIGS_SEL_RTC_WU                 ((uint8_t)0x0C)
#define ADC_TRIGS_SEL_RTC_ALARM              ((uint8_t)0x0D)
#define ADC_TRIGS_SEL_PCA                    ((uint8_t)0x0E)
#define ADC_TRIGS_SEL_SPI                    ((uint8_t)0x0F)
#define ADC_TRIGS_SEL_PA1                    ((uint8_t)0x10)
#define ADC_TRIGS_SEL_PA2                    ((uint8_t)0x11)
#define ADC_TRIGS_SEL_PA3                    ((uint8_t)0x12)
#define ADC_TRIGS_SEL_PB4                    ((uint8_t)0x13)
#define ADC_TRIGS_SEL_PB5                    ((uint8_t)0x14)
#define ADC_TRIGS_SEL_PC3                    ((uint8_t)0x15)
#define ADC_TRIGS_SEL_PC4                    ((uint8_t)0x16)
#define ADC_TRIGS_SEL_PC5                    ((uint8_t)0x17)
#define ADC_TRIGS_SEL_PC6                    ((uint8_t)0x18)
#define ADC_TRIGS_SEL_PC7                    ((uint8_t)0x19)
#define ADC_TRIGS_SEL_PD1                    ((uint8_t)0x1A)
#define ADC_TRIGS_SEL_PD2                    ((uint8_t)0x1B)
#define ADC_TRIGS_SEL_PD3                    ((uint8_t)0x1C)
#define ADC_TRIGS_SEL_PD4                    ((uint8_t)0x1D)
#define ADC_TRIGS_SEL_PD5                    ((uint8_t)0x1E)
#define ADC_TRIGS_SEL_PD6                    ((uint8_t)0x1F)

#define IS_ADC_TRIGS_SEL(SEL)                ((((SEL) >= 0x00) && ((SEL) <= 0x0A)) || \
                                              (((SEL) >= 0x0C) && ((SEL) <= 0x1F)))

/**
  * @}
  */ 



/** @defgroup ADC_thresholds 
  * @{
  */ 
  
#define IS_ADC_THRESHOLD(THRESHOLD) ((THRESHOLD) <= 0xFFF)

/**
  * @}
  */ 
  
/** @defgroup ADC_channels 
  * @{
  */ 
#define ADC_SIGNALChannel_0                        ADC_CR0_CHSEL_CH0
#define ADC_SIGNALChannel_1                        ADC_CR0_CHSEL_CH1
#define ADC_SIGNALChannel_2                        ADC_CR0_CHSEL_CH2
#define ADC_SIGNALChannel_3                        ADC_CR0_CHSEL_CH3
#define ADC_SIGNALChannel_4                        ADC_CR0_CHSEL_CH4
#define ADC_SIGNALChannel_5                        ADC_CR0_CHSEL_CH5
#define ADC_SIGNALChannel_6                        ADC_CR0_CHSEL_CH6
#define ADC_SIGNALChannel_7                        ADC_CR0_CHSEL_CH7

#define IS_ADC_SIGNALCHANNEL(SIGNALCHANNEL)  (((SIGNALCHANNEL) == ADC_SIGNALChannel_0) || ((SIGNALCHANNEL) == ADC_SIGNALChannel_1) ||\
                                              ((SIGNALCHANNEL) == ADC_SIGNALChannel_2) || ((SIGNALCHANNEL) == ADC_SIGNALChannel_3) ||\
                                              ((SIGNALCHANNEL) == ADC_SIGNALChannel_4) || ((SIGNALCHANNEL) == ADC_SIGNALChannel_5) ||\
                                              ((SIGNALCHANNEL) == ADC_SIGNALChannel_6) || ((SIGNALCHANNEL) == ADC_SIGNALChannel_7))


#define ADC_Channel_ALL                            ADC_CR2_CHEN 
#define ADC_Channel_0                              ADC_CR2_CHEN0EN
#define ADC_Channel_1                              ADC_CR2_CHEN1EN
#define ADC_Channel_2                              ADC_CR2_CHEN2EN
#define ADC_Channel_3                              ADC_CR2_CHEN3EN
#define ADC_Channel_4                              ADC_CR2_CHEN4EN
#define ADC_Channel_5                              ADC_CR2_CHEN5EN
#define ADC_Channel_6                              ADC_CR2_CHEN6EN
#define ADC_Channel_7                              ADC_CR2_CHEN7EN

#define IS_ADC_CHANNEL(CHANNEL)             (((CHANNEL) == ADC_Channel_0) || ((CHANNEL) == ADC_Channel_1) ||\
                                             ((CHANNEL) == ADC_Channel_2) || ((CHANNEL) == ADC_Channel_3) ||\
                                             ((CHANNEL) == ADC_Channel_4) || ((CHANNEL) == ADC_Channel_5) ||\
                                             ((CHANNEL) == ADC_Channel_6) || ((CHANNEL) == ADC_Channel_7) ||\
                                             ((CHANNEL) == ADC_Channel_ALL))

/**
  * @}
  */ 
  
/** @defgroup ADC_interrupts_definition 
  * @{
  */ 
#define ADC_IT_CONT                               ADC_INTEN_CONTIEN
#define ADC_IT_REG                                ADC_INTEN_REGIEN
#define ADC_IT_HHT                                ADC_INTEN_HHTIEN
#define ADC_IT_LLT                                ADC_INTEN_LLTIEN
#define ADC_IT_RIS7                               ADC_INTEN_ADCIEN0
#define ADC_IT_RIS6                               ADC_INTEN_ADCIEN6
#define ADC_IT_RIS5                               ADC_INTEN_ADCIEN5
#define ADC_IT_RIS4                               ADC_INTEN_ADCIEN4
#define ADC_IT_RIS3                               ADC_INTEN_ADCIEN3
#define ADC_IT_RIS2                               ADC_INTEN_ADCIEN2
#define ADC_IT_RIS1                               ADC_INTEN_ADCIEN1
#define ADC_IT_RIS0                               ADC_INTEN_ADCIEN0
 
#define IS_ADC_CONFIG_IT(IT) (((IT) != (uint32_t)RESET) && (((IT) & 0xFFFFF000) == (uint32_t)RESET))

#define IS_ADC_GET_IT(IT) (((IT) == ADC_IT_CONT)|| ((IT) == ADC_IT_REG) || \
                           ((IT) == ADC_IT_HHT) || ((IT) == ADC_IT_LLT) || \
                           ((IT) == ADC_IT_RIS7)|| ((IT) == ADC_IT_RIS6)|| \
                           ((IT) == ADC_IT_RIS5)|| ((IT) == ADC_IT_RIS4)|| \
                           ((IT) == ADC_IT_RIS3)|| ((IT) == ADC_IT_RIS2)|| \
                           ((IT) == ADC_IT_RIS1)|| ((IT) == ADC_IT_RIS0))

#define IS_ADC_CLEAR_IT(IT) (((IT) != (uint32_t)RESET) && (((IT) & 0xFFFFF000) == (uint32_t)RESET))

/**
  * @}
  */ 

/** @defgroup ADC_flags_definition 
  * @{
  */ 

#define ADC_FLAG_ADRDY                              (uint32_t)(0x02000000|ADC_CR2_READY)                                      
#define ADC_FLAG_START                              (uint32_t)(0x01000000|ADC_CR0_START)  
#define ADC_FLAG_CONT                               ADC_RAWINTSR_CONT
#define ADC_FLAG_REG                                ADC_RAWINTSR_REG
#define ADC_FLAG_HHT                                ADC_RAWINTSR_HHT
#define ADC_FLAG_LLT                                ADC_RAWINTSR_LLT
#define ADC_FLAG_RIS7                               ADC_RAWINTSR_ADCI7
#define ADC_FLAG_RIS6                               ADC_RAWINTSR_ADCI6
#define ADC_FLAG_RIS5                               ADC_RAWINTSR_ADCI5
#define ADC_FLAG_RIS4                               ADC_RAWINTSR_ADCI4
#define ADC_FLAG_RIS3                               ADC_RAWINTSR_ADCI3
#define ADC_FLAG_RIS2                               ADC_RAWINTSR_ADCI2
#define ADC_FLAG_RIS1                               ADC_RAWINTSR_ADCI1
#define ADC_FLAG_RIS0                               ADC_RAWINTSR_ADCI0

#define IS_ADC_CLEAR_FLAG(FLAG) ((((FLAG) != (uint32_t)RESET) && (((FLAG) & 0xFFFFF000) == (uint32_t)RESET))||\
                                 ((FLAG) == ADC_FLAG_ADRDY) || ((FLAG) == ADC_FLAG_START))

#define IS_ADC_GET_FLAG(FLAG) (((FLAG) == ADC_FLAG_CONT) || ((FLAG) == ADC_FLAG_REG) || \
                               ((FLAG) == ADC_FLAG_HHT)  || ((FLAG) == ADC_FLAG_LLT) || \
                               ((FLAG) == ADC_FLAG_RIS7) || ((FLAG) == ADC_FLAG_RIS6)   || \
                               ((FLAG) == ADC_FLAG_RIS5) || ((FLAG) == ADC_FLAG_RIS4)|| \
                               ((FLAG) == ADC_FLAG_RIS3) || ((FLAG) == ADC_FLAG_RIS2)|| \
                               ((FLAG) == ADC_FLAG_RIS1) || ((FLAG) == ADC_FLAG_RIS0)|| \
                               ((FLAG) == ADC_FLAG_START)|| ((FLAG) == ADC_FLAG_ADRDY))
/**
  * @}
  */  


/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */ 

/*  Function used to set the ADC configuration to the default reset state *****/
void ADC_DeInit(void);

/* Initialization and Configuration functions *********************************/ 
ErrorStatus ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct);
void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct);
void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState);
void ADC_SetStartupTime(ADC_TypeDef* ADCx, uint32_t ADC_CLK);
void ADC_DiscardCmd(ADC_TypeDef* ADCx, FunctionalState NewState);

/* Analog Watchdog configuration functions ************************************/
void ADC_ThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,uint16_t LowThreshold);
void ADC_RegCompareConfig(ADC_TypeDef* ADCx, FunctionalState NewState);
void ADC_HHtCompareConfig(ADC_TypeDef* ADCx, FunctionalState NewState);
void ADC_LLtCompareConfig(ADC_TypeDef* ADCx, FunctionalState NewState);

void ADC_AccCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
void ADC_AccResultClear(ADC_TypeDef* ADCx);

/* Channels Configuration functions *******************************************/
void ADC_ChannelConfig(ADC_TypeDef* ADCx, uint16_t ADC_Chn);
void ADC_ContinuousModeChannelConfig(ADC_TypeDef* ADCx, uint16_t ADC_Chn);
void ADC_ContinuousModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
void ADC_SetContinuousConversionNumber(ADC_TypeDef* ADCx, uint8_t Number);
void ADC_SetContinuousBurstSampleTime(ADC_TypeDef* ADCx, uint8_t SampleTime);

void ADC_StartOfConversion(ADC_TypeDef* ADCx);
uint32_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
uint32_t ADC_GetChnResultValue(ADC_TypeDef* ADCx, uint16_t ADC_Chn);
uint32_t ADC_GetAccResultValue(ADC_TypeDef* ADCx);

/* Regular Channels DMA Configuration functions *******************************/
void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState);

/* Interrupts and flags management functions **********************************/
void ADC_ITConfig(ADC_TypeDef* ADCx, uint32_t ADC_IT, FunctionalState NewState);
FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint32_t ADC_FLAG);
void ADC_ClearFlag(ADC_TypeDef* ADCx, uint32_t ADC_FLAG);
ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint32_t ADC_IT);
void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint32_t ADC_IT);

#ifdef __cplusplus
}
#endif


#endif /*__tc32l010_ADC_H */

/************************ (C) COPYRIGHT CHIPAT *****END OF FILE****/
